<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" omit-xml-declaration="yes"/>

    <!-- Platilla principal -->
    <xsl:template match="/">
        <xsl:variable name="mins">abcdefghijklmnopqrstuvwxyz</xsl:variable>
        <xsl:variable name="mays">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

        // Declaración de paquete
        package <xsl:value-of select="entidad/paquete"/>;
        
        // Importaciones
        import java.beans.PropertyChangeListener;
        import java.beans.PropertyChangeSupport;
        import java.io.Serializable;
        
        // Cabecera de la clase
        public class <xsl:value-of select="entidad/nombre"/> implements Serializable {

        // Identificadores de propiedades
        <xsl:for-each select="entidad/campos/campo">
            <xsl:text>public static final String PROP_</xsl:text>
            <xsl:value-of select="upper-case(@nombre)"/>
            <xsl:text> = &quot;</xsl:text>
            <xsl:value-of select="@nombre"/>
            <xsl:text>&quot;</xsl:text>;
        </xsl:for-each>    
      
        // Propiedades
        <xsl:for-each select="entidad/campos/campo">
            <xsl:text>private </xsl:text>
            <xsl:value-of select="@tipo"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="@nombre"/>;
        </xsl:for-each>    

        // PropertyChangeSupport
        private final transient PropertyChangeSupport propertyChangeSupport = new java.beans.PropertyChangeSupport(this);

        // Accesores
        <xsl:for-each select="entidad/campos/campo">
            <xsl:variable name="nombre" select="concat(translate(substring(@nombre,1,1), $mins, $mays), substring(@nombre,2))"/>
            <xsl:text>public void set</xsl:text>
            <xsl:value-of select="$nombre"/>(<xsl:value-of select="@tipo"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="@nombre"/>) {
            <xsl:value-of select="@tipo"/> old<xsl:value-of select="$nombre"/> = this.<xsl:value-of select="@nombre"/>;
            this.<xsl:value-of select="@nombre"/> = <xsl:value-of select="@nombre"/>;
            <xsl:text>propertyChangeSupport.firePropertyChange(PROP_</xsl:text>
            <xsl:value-of select="upper-case(@nombre)"/>
            <xsl:text>, old</xsl:text>
            <xsl:value-of select="$nombre"/>, <xsl:value-of select="@nombre"/>);
            }

            public <xsl:value-of select="@tipo"/> get<xsl:value-of select="@nombre"/>() {
            return <xsl:value-of select="@nombre"/>;
            }
        </xsl:for-each>    

        // Soporte para listeners
        public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
        }
        }
    </xsl:template>
</xsl:stylesheet>
